"""
使用httpx请求代理服务器
请求http和https网页均适用
"""
import asyncio
import httpx
import requests

# 应用账号（请替换为真实账号）
app_key = 'app_key'
# 应用密码（请替换为真实密码）
app_secret = 'app_secret'

api_url = "https://api.xiaoxiangdaili.com/ip/get"

# 要访问的目标网页
target_url = "http://httpbin.org/ip"

url = api_url + "?appKey=" + app_key + "&appSecret=" + app_secret + "&cnt=1&wt=text"

# API接口返回的proxy_list
proxy_list = requests.get(url).text
print("API response: " + proxy_list)


async def fetch(url):
    proxies = {
        "http://": f"http://{app_key}:{app_secret}@{proxy_list}",
        "https://": f"http://{app_key}:{app_secret}@{proxy_list}",
    }
    async with httpx.AsyncClient(proxies=proxies, timeout=10) as client:
        resp = await client.get(url)
        print(f"status_code: {resp.status_code}, content: {resp.content}")


loop = asyncio.get_event_loop()
tasks = [fetch(target_url) for _ in range(1)]
loop.run_until_complete(asyncio.wait(tasks))